/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ucm;
import sql.*;
import java.sql.*;
import proxy.Proxy;
import thread.Server_Thread;
/**
 *
 * @author Donghui
 */
public class SearchInfo {
    private static SearchInfo local;
    private String msg;
    private String sql;
    private String[] data;
    private String[] record=new String[10];
    private ResultSet result;
    private String info;
    private Server_Thread st;


    public static SearchInfo getInstance(String tp, Server_Thread st)
    {
        if(local==null)
        {
            local=new SearchInfo();
            local.msg=tp;
            local.st=st;
        }
        else
        {
            local.st=st;
            local.msg=tp;
        }
        return local;
    }
    
    
    public void searchStudent()
    {
        try{
        data=msg.split(";");
        sql="SELECT * FROM subjectachievement WHERE student_id='"+data[0]+"'";
        result=Select.getInstance().selectValue(sql);
        while(result.next())
        {
            record[0]=result.getString("course_id");
            record[1]=result.getString("teacher_id");
            record[2]=Float.toString(result.getFloat("mark"));
            record[3]=result.getString("failed");
            record[4]=result.getString("marked_date");
            info="Y;"+record[0]+";"+record[1]+";"+record[2]+";"+record[3]+";"+record[4]+";01";
            Proxy.getInstance(st).output(info);
            Thread.sleep(500);
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void searchTeacher()
    {
        try{
        data=msg.split(";");
        sql="SELECT * FROM course WHERE teacher_id='"+ data[0] +"'";
        result=Select.getInstance().selectValue(sql);
        while(result.next())
        {
            record[0]=result.getString("course_id");
            record[1]=result.getString("subject");
            record[2]=String.valueOf(result.getInt("credit_hour"));
            record[3]=result.getString("description");
            record[4]=result.getString("college");
            info="Y;"+record[0]+";"+record[1]+";"+record[2]+";"+record[3]+";"+record[4]+";06";
            Proxy.getInstance(st).output(info);
//            new Proxy(st).output(info);
            Thread.sleep(500);
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }


}
